package cn.bingoo.nginx;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Millisecond;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;

public class NginxStatusPanel extends JPanel
{
    private static final long serialVersionUID = 9002466137986895193L;

    private int totalActiveCollection = 0;

    class DataGenerator extends Timer implements ActionListener
    {

        private static final long serialVersionUID = 8053122743795474095L;

        @Override
        public void actionPerformed(ActionEvent actionevent)
        {
            NginxStatusClient nginxStatusClient = new NginxStatusClient();
            totalActiveCollection = 0;
            for (int i = 0; i < NginxConfig.URLS.length; i++) {
                ArrayList<String> collect = null;
                try {
                    collect = nginxStatusClient.collect(NginxConfig.URLS[i]);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                totalActiveCollection += Integer.parseInt(collect.get(1));
                addTimeSeries(i, collect);
            }
            jfreechart.setTitle("Nginx Status Monitor " + totalActiveCollection);
        }

        DataGenerator(int i)
        {
            super(i, null);
            addActionListener(this);
        }
    }

    @SuppressWarnings("deprecation")
    public NginxStatusPanel(int i)
    {
        super(new BorderLayout());
        TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
        for (String url : NginxConfig.URLS) {
            TimeSeries e = new TimeSeries(url, org.jfree.data.time.Millisecond.class);
            allTimeSeries.add(e);
            timeseriescollection.addSeries(e);
        }
        DateAxis dateaxis = new DateAxis("Time");
        NumberAxis numberaxis = new NumberAxis("Nginx Active Connections");
        dateaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
        numberaxis.setTickLabelFont(new Font("SansSerif", 0, 12));
        dateaxis.setLabelFont(new Font("SansSerif", 0, 14));
        numberaxis.setLabelFont(new Font("SansSerif", 0, 14));
        XYLineAndShapeRenderer xylineandshaperenderer = new XYLineAndShapeRenderer(true, false);
        xylineandshaperenderer.setSeriesPaint(0, Color.red);
        xylineandshaperenderer.setSeriesPaint(1, Color.green);
        xylineandshaperenderer.setSeriesStroke(0, new BasicStroke(3F, 0, 2));
        xylineandshaperenderer.setSeriesStroke(1, new BasicStroke(3F, 0, 2));
        XYPlot xyplot = new XYPlot(timeseriescollection, dateaxis, numberaxis, xylineandshaperenderer);
        xyplot.setBackgroundPaint(Color.lightGray);
        xyplot.setDomainGridlinePaint(Color.white);
        xyplot.setRangeGridlinePaint(Color.white);
        xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
        dateaxis.setAutoRange(true);
        dateaxis.setLowerMargin(0.0D);
        dateaxis.setUpperMargin(0.0D);
        dateaxis.setTickLabelsVisible(true);
        numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        jfreechart = new JFreeChart("Nginx Status Monitor", new Font("SansSerif", 1, 24), xyplot, true);
        jfreechart.setBackgroundPaint(Color.white);
        ChartPanel chartpanel = new ChartPanel(jfreechart, true);
        chartpanel.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4),
                BorderFactory.createLineBorder(Color.black)));
        add(chartpanel);
    }

    public static void main(String args[])
    {
        JFrame jframe = new JFrame("Memory Usage Demo");
        NginxStatusPanel memoryusagedemo = new NginxStatusPanel(30000);
        jframe.getContentPane().add(memoryusagedemo, "Center");
        jframe.setBounds(200, 120, 600, 280);
        jframe.setVisible(true);
        memoryusagedemo.new DataGenerator(100).start();
        jframe.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowevent)
            {
                System.exit(0);
            }
        });
    }

    public void addTimeSeries(int i, ArrayList<String> collect) {
        TimeSeries timeSeries = getAllTimeSeries().get(i);
        timeSeries.add(new Millisecond(), Double.parseDouble(collect.get(1)));
    }

    private JFreeChart jfreechart = null;

    private List<TimeSeries> allTimeSeries = new ArrayList<TimeSeries>();

    public List<TimeSeries> getAllTimeSeries() {
        return allTimeSeries;
    }

    public void setAllTimeSeries(List<TimeSeries> allTimeSeries) {
        this.allTimeSeries = allTimeSeries;
    }

}
